<script setup lang="ts">
import { reactive, ref } from "vue";
import useUserStore from "@/store/modules/user";
import { useRouter, useRoute } from "vue-router";
import { ElNotification } from "element-plus";
import { getTime } from "@/utils/time";
const userStore = useUserStore();
const $route = useRoute();
let $router: any = useRouter();
// 用于表单验证的规则
const validatorUserName = (rule: any, value: any, callback: any) => {
  if (value.length >= 5 && value.length <= 10) {
    callback();
  } else {
    callback(new Error("账号必须在五到十位之间"));
  }
};
const validatorPassword = (rule: any, value: any, callback: any) => {
  if (value.length >= 5 && value.length <= 10) {
    callback();
  } else {
    callback(new Error("密码必须在五到十位之间"));
  }
};
const rules = {
  username: [{ trigger: "change", validator: validatorUserName }],
  password: [{ trigger: "blur", validator: validatorPassword }],
};
// el-form的组件实例
const loginForms = ref();
const loginForm = reactive({ username: "admin", password: "atguigu123" });
// 该字段用于控制按钮loading效果的开启和关闭
let loading = ref(false);
const login = async () => {
  await loginForms.value.validate();
  loading.value = true;
  try {
    await userStore.userLogin(loginForm);
    loading.value = false;
    // 登录回到退出登录的页面
    $router.push({ path: $route.query.redirect || "/" });
    ElNotification({
      title: `Hi,${getTime()}`,
      message: "登录成功,欢迎回来",
      type: "success",
      duration: 2500,
    });
  } catch (error) {
    loading.value = false;
    ElNotification({
      type: "error",
      message: (error as Error).message,
    });
  }
};
</script>

<template>
  <div class="login-container">
    <el-row>
      <el-col :span="12"></el-col>
      <el-col :span="12" class="right-box">
        <h1 class="title">Hello</h1>
        <h2 class="introduce">欢迎来到我的世界</h2>
        <el-form
          class="form-list"
          :model="loginForm"
          :rules="rules"
          ref="loginForms"
        >
          <el-form-item prop="username">
            <el-input
              placeholder="请输入用户名"
              type="text"
              prefix-icon="User"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              placeholder="请输入密码"
              type="password"
              prefix-icon="Lock"
              show-password
              v-model="loginForm.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              class="el-btn"
              @click="login"
              :loading="loading"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped lang="scss">
.login-container {
  width: 100%;
  height: 100vh;
  background-color: pink;
  background: url("@/assets/images/background.jpg") no-repeat;
  background-size: cover;

  .right-box {
    margin-top: 30vh;
    background: url("@/assets/images/login_form.png");
    background-size: cover;
    padding: 40px;

    .title {
      color: white;
    }

    .introduce {
      color: white;
      margin: 20px 0;
    }

    .form-list {
      margin: 0 70px;
    }

    .el-btn {
      width: 100%;
    }
  }
}
</style>
